const EventEmitter = require("events");
var events=require ("events");
const { clearInterval } = require("timers");
var util = require('util');
var Emitter=events.EventEmitter;//构造函数
function Dog(obj) {//构造函数名字大写
    Emitter.call(this);
    this.name=obj.name;
    this.energy=obj.energy;
    // var id=setInterval(() => {
    //     if(this.energy>-1){  
    //         this.emit();       
    //     }else{
    //         clearInterval(id);
    //     }            
    // }, 1000);
    
}
// Dog.prototype=Emitter.prototype;//通过prototype实现对EventEmitter的继承
for(var i in EventEmitter.prototype){
    Dog.prototype[i]=EventEmitter.prototype[i];
}
Dog.prototype.bark=function () {   
    if(this.energy>-1){        
        console.log(this.name+" barked!"+"   energy: "+this.energy);
        this.energy--;
    }        
}
// util.inherits(Dog,Emitter);

module.exports=Dog;//对外公布Dog构造函数